﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

using ESOL.Common.Entity;
using ESOL.Business.Pur;
using ESOL.Business.Chk;

namespace ESOL.WinUI.Qc
{
    public partial class frmMaintainIncomingQcApprove : BaseForm.frmBaseDialogyMaintainForm
    {
        #region Variable
        private QcIqcOrderEntity mSelectedIqcOrder;
        private PurRejectedOrderEntity mSelectedRejectedOrder;
        public string ActionType { set; get; }
        #endregion

        #region Constructor
        public frmMaintainIncomingQcApprove()
        {
            InitializeComponent();
        }

        public frmMaintainIncomingQcApprove(QcIqcOrderEntity entity)
        {
            InitializeComponent();
            mSelectedIqcOrder = entity;
        }
        #endregion

        #region Functions
        private void Init()
        {
            if (ActionType.Equals("New"))
            {
                btnSave.Enabled = true;
                btnConfirm.Enabled = false;
            }
            else if (ActionType.Equals("Confirm"))
            {
                btnSave.Enabled = false;
                btnConfirm.Enabled = true;
            }
        }
        private void BindComobox()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("name");
            dt.Columns.Add("value", typeof(int));
            dt.Rows.Add("部分拒收", 0);
            dt.Rows.Add("整单拒收", 1);
            cmbReturnFunction.DataSource = dt;
            cmbReturnFunction.DisplayMember = "name";
            cmbReturnFunction.ValueMember = "value";
        }
        private void SetField()
        {
            if (mSelectedIqcOrder == null)
                return;
            if (ActionType.Equals("New"))
            {
                txtIqcOrder.Text = mSelectedIqcOrder.OrderCode;
                PurPurchaseArrivedEntity mPurchaseArrived = PurPurchaseArrivedBs.GetPurchaseArrivedByID(mSelectedIqcOrder.PurArrivedId);
                if (mPurchaseArrived != null)
                {
                    txtArrivedOrder.Text = mPurchaseArrived.ArrivedCode;
                }
                cmbReturnFunction.SetSelectedValue(0);
            }
            if (ActionType.Equals("Confirm"))
            {
                txtIqcOrder.Text = mSelectedIqcOrder.OrderCode;
                PurPurchaseArrivedEntity mPurchaseArrived = PurPurchaseArrivedBs.GetPurchaseArrivedByID(mSelectedIqcOrder.PurArrivedId);
                if (mPurchaseArrived != null)
                {
                    txtArrivedOrder.Text = mPurchaseArrived.ArrivedCode;
                }
                mSelectedRejectedOrder = PurRejectedOrderBs.GetRejectedOrderByIqcOrderID(mSelectedIqcOrder.OrderId);
                if (mSelectedRejectedOrder != null)
                {
                    txtRemark.Text = mSelectedRejectedOrder.Remark;
                }
                cmbReturnFunction.SetSelectedValue(mSelectedRejectedOrder.RejectedType);
            }

        }
        private void BindGrid()
        {
            if (mSelectedIqcOrder == null)
                return;
            switch (ActionType)
            {
                case "New":
                    DataTable dt = QcIqcOrderItemDetailBs.GetDtNgDetailByOrderID(mSelectedIqcOrder.OrderId);
                    dt.Columns.Add("REJECTED_QTY", typeof(decimal));
                    foreach (DataRow dr in dt.Rows)
                    {
                        dr["REJECTED_QTY"] = dr["QTY"];
                    }
                    gvMain.DataSource = dt;
                    break;
                case "Confirm":
                    DataTable dt1 = PurRejectedOrderItemBs.GetDtAllByOrderID(mSelectedRejectedOrder.RejectedOrderId);
                    dt1.Columns.Add("REJECTED_QTY", typeof(decimal));
                    foreach (DataRow dr in dt1.Rows)
                    {
                        dr["REJECTED_QTY"] = dr["QTY"];
                    }
                    gvMain.DataSource = dt1;
                    break;
            }
        }
        private void SetEntity()
        {
            if (mSelectedIqcOrder == null)
                return;
            mSelectedRejectedOrder = new PurRejectedOrderEntity();
            mSelectedRejectedOrder.RejectedOrderId = Guid.NewGuid().ToString();
            mSelectedRejectedOrder.ArrivedId = mSelectedIqcOrder.PurArrivedId;
            mSelectedRejectedOrder.IqcOrderId = mSelectedIqcOrder.OrderId;
            mSelectedRejectedOrder.RejectedType = Convert.ToInt32(cmbReturnFunction.SelectedValue.ToString());
            mSelectedRejectedOrder.RejectedPerson = Common.CommonParameter.CurrentUser.PersonId;
            mSelectedRejectedOrder.RejectedDate = DateTime.Now;
            mSelectedRejectedOrder.CreatedDate = DateTime.Now;
            mSelectedRejectedOrder.CreatedBy = Common.CommonParameter.CurrentUser.PersonId;
            mSelectedRejectedOrder.LastModifiedDate = DateTime.Now;
            mSelectedRejectedOrder.LastModifiedBy = Common.CommonParameter.CurrentUser.PersonId;
            mSelectedRejectedOrder.Remark = txtRemark.Text;
        }
        private bool SaveRejectedOrder()
        {
            SetEntity();
            List<PurRejectedOrderItemEntity> list = new List<PurRejectedOrderItemEntity>();
            if (cmbReturnFunction.SelectedValue.ToString().Equals("0"))
            {
                DataTable dt = (DataTable)gvMain.DataSource;
                foreach (DataRow dr in dt.Rows)
                {
                    PurRejectedOrderItemEntity m = new PurRejectedOrderItemEntity();
                    m.RejectedOrderItemId = Guid.NewGuid().ToString();
                    m.RejectedOrderId = mSelectedRejectedOrder.RejectedOrderId;
                    m.ArrivedItemId = dr["ARRIVED_ITEM_ID"].ToString();
                    m.Qty = Convert.ToDecimal(dr["REJECTED_QTY"].ToString());
                    list.Add(m);
                }
            }
            else if (cmbReturnFunction.SelectedValue.ToString().Equals("1"))
            {
                DataTable dt = PurPurchaseArrivedItemBs.GetDtAllByArrivedID(mSelectedIqcOrder.PurArrivedId);
                foreach (DataRow dr in dt.Rows)
                {
                    PurRejectedOrderItemEntity m = new PurRejectedOrderItemEntity();
                    m.RejectedOrderItemId = Guid.NewGuid().ToString();
                    m.RejectedOrderId = mSelectedRejectedOrder.RejectedOrderId;
                    m.ArrivedItemId = dr["ARRIVED_ITEM_ID"].ToString();
                    m.Qty = Convert.ToDecimal(dr["ARRIVED_QTY"].ToString());
                    list.Add(m);
                }
            }
            if (PurRejectedOrderBs.CreateRejectOrder(mSelectedRejectedOrder, list))
            {
                return true;
            }
            else
            {
                MessageBox.Show("审核失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        #endregion

        private void frmMaintainIncomingQcApprove_Load(object sender, EventArgs e)
        {
            Init();
            BindComobox();
            SetField();
            BindGrid();
        }

        private void cmbReturnFunction_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbReturnFunction.SelectedValue == null)
                return;
            if (string.IsNullOrEmpty(cmbReturnFunction.SelectedValue.ToString()))
                return;
            if (cmbReturnFunction.SelectedValue.ToString().Equals("1"))
            {
                gvMain.Enabled = false;
            }
            else
            {
                gvMain.Enabled = true;
            }
        }

        protected override void Click_Save()
        {
            if (SaveRejectedOrder())
            {
                MessageBox.Show("审核成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                DialogResult = DialogResult.OK;
                this.Dispose();
            }
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            if (mSelectedRejectedOrder != null)
            {
                mSelectedIqcOrder.Status = "STATUS06";
                if (QcIqcOrderBs.IqcOrderConfirm(mSelectedIqcOrder))
                {
                    MessageBox.Show("确认成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    DialogResult = DialogResult.OK;
                    this.Dispose();
                }
                else
                {
                    MessageBox.Show("确认失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }


    }
}